/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.demister.sgt.cmd;

import java.awt.Graphics;

/**
 * GraphicsCommand is command for java.awt.Graphics or Grahpics2D.
 * It provides the functions such as excute, undo, generate code.
 * It is indentified by command name, so make sure name is unique.
 * Notice that the undoable attribute is false if and only if undo method throw UnsupportedOperationException e.
 * The default undo implementation is throw such exception.
 *
 * @author Rock
 */
public abstract class GraphicsCommand {

    /**
     * Grahpics source
     */
    protected Graphics graphics;
    /**
     * Command name
     */
    protected String name;
    /**
     * Undoable attribute
     * Set in constructor.
     */
    protected boolean undoable;

    /**
     * Creates an instance of GraphicsCommand of empty name, and null graphics source.
     */
    protected GraphicsCommand() {
        this(null, "");
    }

    /**
     * Creates an instance of GraphicsCommand of givenname, and given graphics source.
     *
     * @param graphics the graphics source
     * @param name the command name
     */
    protected GraphicsCommand(Graphics graphics, String name) {
        this.graphics = graphics;
        this.name = name;
        undoable = true;
        try {
            this.undo();
        } catch (UnsupportedOperationException e) {
            undoable = false;
        }
    }

    /**
     * Returns graphics source.
     *
     * @return the graphics source
     */
    public Graphics getGraphics() {
        return graphics;
    }

    /**
     * Sets graphics source.
     *
     * @param graphics the new graphics source
     */
    public void setGraphics(Graphics graphics) {
        this.graphics = graphics;
    }

    /**
     * Executes this command.
     *
     * @return true if successful; false if not
     */
    public abstract boolean execute();

    /**
     * Undos this executed command.
     * The default implementation is throw exception.
     * Notice that the method should have no effect if the command has not been executed.
     *
     * @throws java.lang.UnsupportedOperationException if the command is not undoable
     */
    public void undo() throws UnsupportedOperationException {
        throw new UnsupportedOperationException(
                this.getName() + " Command: Not supported yet.");
    }

    /**
     * Returns the java code of this command.
     * Variable g represents the graphics source.
     *
     * @return the java code
     */
    public abstract String getCode();

    /**
     * Returns whether this command is undoable.
     *
     * @return true if undoable; false if not
     */
    public boolean isUndoable() {
        return undoable;
    }

    /**
     * Returns this command name.
     *
     * @return
     */
    public String getName() {
        return this.name;
    }

    @Override
    /**
     * Returns the java code of this command.
     *
     * @return the java code
     */
    public String toString() {
        return this.getCode();
    }
}